package org.hospital.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hospital.entity.Clinic;
import org.hospital.entity.Department;
import org.hospital.entity.Employee;
import org.hospital.entity.EmployeeDAO;
import org.hospital.entity.Project;
import org.hospital.entity.ProjectDAO;
import org.hospital.service.ProjectService;
import org.hospital.util.Constant;
import org.hospital.util.DoubleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Service
public class ProjectServiceImpl implements ProjectService {

	@Autowired
	private ProjectDAO pjDao;
	@Autowired
	private EmployeeDAO eDao;

	@Override
	public List<Project> getClinicTreatmentProject(Clinic clinic, long startTime, long endTime) {
		return pjDao.getClinicTreatmentProject(clinic.getClinicId(), startTime, endTime);
	}
	
	@Override
	public List<Project> getDepartmentTreatmentProject(int departmentId, long startTime, long endTime) {
		return pjDao.getDepartmentTreatmentProject(departmentId, startTime, endTime);
	}

	@Override
	public Project getById(int projectId) {
		return pjDao.findById(projectId);
	}

	@Override
	public Project addEntity(Project entity) {
		return pjDao.merge(entity);
	}

	@Override
	public Project saveEntity(Project entity) {
		return pjDao.merge(entity);
	}

	@Override
	public List<Project> getAllTreatmentProject() {
		Map paraMap = new HashMap<>();
		paraMap.put("category", Constant.TREATMENT_PROJECT.getName());
		paraMap.put("status", 0);
		return pjDao.getByProperties(paraMap);
	}

	@Override
	public List<Project> getAllFundProject() {
		Map paraMap = new HashMap<>();
		paraMap.put("category", Constant.FUND_PROJECT.getName());
		paraMap.put("status", 0);
		return pjDao.getByProperties(paraMap);
	}

	@Override
	public JSONArray getTreatTable() {
		JSONArray resultJA = new JSONArray();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		List<Project> pjList = pjDao.findByCategory(Constant.TREATMENT_PROJECT.getName());
		for (int i = 0; i < pjList.size(); i++) {
			Project pj = pjList.get(i);
			JSONObject resultJO = new JSONObject();
			Employee creator = eDao.findById(pj.getCreatorId());
			resultJO.put("projectId", pj.getProjectId());
			resultJO.put("projectName", pj.getName());
			resultJO.put("creatorId",creator.getEmployeeId());
			resultJO.put("creatorName", creator.getName());
			resultJO.put("unitPrice", DoubleUtil.parseDoubleTwo(pj.getUnitPrice()));
			resultJO.put("doctorCost", DoubleUtil.parseDoubleTwo(pj.getDoctorCost()));
			resultJO.put("clinicCost", DoubleUtil.parseDoubleTwo(pj.getClinicCost()));
			resultJO.put("excutorCost", DoubleUtil.parseDoubleTwo(pj.getExcutorCost()));
			if(pj.getType().equals(0)) {
				resultJO.put("type", "收入");
			}else if(pj.getType().equals(1)) {
				resultJO.put("type", "支出");
			}
			resultJO.put("typeInt", pj.getType());
			
			if(pj.getStatus().equals(1)) {
				resultJO.put("status", "禁用");
			}else if(pj.getStatus().equals(0)) {
				resultJO.put("status", "启用");
			}
			resultJO.put("statusInt", pj.getStatus());
			
			resultJO.put("alterTime", sdf.format(pj.getAlterTime()));
			resultJO.put("createTime", sdf.format(pj.getCreateTime()));
			resultJA.add(resultJO);
		}
		
		return resultJA;
	}

	@Override
	public JSONArray getFundTable() {
		JSONArray resultJA = new JSONArray();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		List<Project> pjList = pjDao.findByCategory(Constant.FUND_PROJECT.getName());
		//System.out.println("pjService.getFundTable"+JSON.toJSONString(pjList));
		
		for (int i = 0; i < pjList.size(); i++) {
			Project pj = pjList.get(i);
			JSONObject resultJO = new JSONObject();
			Employee creator = eDao.findById(pj.getCreatorId());
			//[{projectId,projectName,creatorName,creatorId,type,status,alterTime,createTime}]
			resultJO.put("projectId", pj.getProjectId());
			resultJO.put("projectName", pj.getName());
			resultJO.put("creatorId",creator.getEmployeeId());
			resultJO.put("creatorName", creator.getName());
			if(pj.getType().equals(0)) {
				resultJO.put("type", "收入");
			}else if(pj.getType().equals(1)) {
				resultJO.put("type", "支出");
			}
			resultJO.put("typeInt", pj.getType());
			
			if(pj.getStatus().equals(1)) {
				resultJO.put("status", "禁用");
			}else if(pj.getStatus().equals(0)) {
				resultJO.put("status", "启用");
			}
			resultJO.put("statusInt", pj.getStatus());
			resultJO.put("alterTime", sdf.format(pj.getAlterTime()));
			resultJO.put("createTime", sdf.format(pj.getCreateTime()));
			resultJA.add(resultJO);
		}
		return resultJA;
	}


}
